<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category CategoryService.php
 * @Depiction:
 * Author: hwy
 * Date: 2021/2/20  16:54
 * +--------------------------------------------------------+
 */

namespace App\Services;


use App\Exceptions\EmptyException;
use App\Exceptions\ResponseException;
use App\Models\Article;
use App\Models\Category;

class CategoryService extends BaseService
{
    protected $category;
    protected $article;

    public function __construct()
    {
        $this->category = new Category();
        $this->article = new Article();
    }

    /**
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/22  13:41
     */
    public function getOne($params)
    {
        return $this->category->find($params);
    }

    /**
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/22  13:41
     */
    public function getList($params)
    {
        $query = $this->category->query();
        $query   = $this->paramsSet($params, $query);
        $list = $query->orderBy('order', 'asc')->get();

        $data                = sort_data_list($list->toArray());
        $return['data']      = $data;
        $return['show_type'] = 1;

        return return_success_msg(ResponseException::REQUEST_SUCCESS_MESSAGE, $return);
    }

    /**
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/22  13:41
     */
    public function addData($params)
    {
        $add_data = $params->input();
        $add_data['status'] = $this->commonSetStatusValue($params->status);

        try {
            $add = $this->category->create($add_data);
        } catch (\Exception $e) {
            return return_error_msg($e->getMessage());
        }

        if ($add) {
            return return_success_msg(ResponseException::SUCCESS_ADD_MESSAGE);
        } else {
            return return_error_msg(ResponseException::ERROR_ADD_MESSAGE);
        }
    }

    /**
     * @param $params
     * @param $id
     * @return mixed
     * @throws EmptyException
     * @Depiction
     * @Author hwy
     * @Date  2021/2/22  16:54
     */
    public function editData($params, $id)
    {
        $data = $this->getOne($id);
        if (empty($data)) {
            throw new EmptyException(ResponseException::DATA_NOT_EXIST_MESSAGE);
        }
        $edit_data = $params->input();
        $edit_data['status'] = $this->commonSetStatusValue($params->status);
        try {
            $edit = $data->update($edit_data);
        } catch (\Exception $e) {
            return return_error_msg($e->getMessage());
        }
        if ($edit) {
            return return_success_msg(ResponseException::SUCCESS_EDIT_MESSAGE);
        } else {
            return return_error_msg(ResponseException::ERROR_EDIT_MESSAGE);
        }
    }

    /**
     * @param $id
     * @return mixed
     * @throws EmptyException
     * @Depiction
     * @Author hwy
     * @Date  2021/2/22  16:54
     */
    public function delData($id)
    {
        $data = $this->getOne($id);
        if (empty($data)) {
            throw new EmptyException(ResponseException::DATA_NOT_EXIST_MESSAGE);
        }

        $article_count = $this->article->where('cid', $id)->count();
        if ($article_count) {
            return return_error_msg('请先删除该分类下的文章');
        }

        //删除子分类
        $child = $this->category->where('pid', $id)->count();
        if ($child) {
            return return_error_msg('存在子分类，请先删除子分类');
        }
        try {
            $del = $data->delete();
        } catch (\Exception $e) {
            return return_error_msg($e->getMessage());
        }

        if ($del) {
            return return_success_msg(ResponseException::SUCCESS_DELETE_MESSAGE);
        } else {
            return return_error_msg(ResponseException::ERROR_DELETE_MESSAGE);
        }

    }

    /**
     * @return array
     * @Depiction
     * @Author hwy
     * @Date  2021/2/22  16:55
     */
    public function getCateLevel()
    {
        $category = $this->category->orderBy('order', 'asc')->get();

        $data = sort_data_list($category->toArray());
        return $data;
    }

}
